package base.oop;

/**
 * @Author Fizz Pu
 * @Date 2021/4/5 下午3:40
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */
public class WaveForm {
    private static long counter;
    private final long id = counter++;

    @Override
    public String toString(){
        return "WaveForm" + id;
    }

    // 现在的需求就是Filter用于Apply.apply(Processor, String);
    // 关键是Processor和Apply之间耦合的太紧了, String改为Object, Processor如果改为Object就不简明了
    // 1. 让客户端程序员按照要求去书写自己的Filter, 可以在上层定义Processor接口, 客户端强制实现Processor,
    // 这就是接口的魅力, 现在才理解了这句话, 上层定义规范, 就可以不用去管怎么实现
    // 2. 很多时候, Filter已经写好了, 没法修改, 我们可以利用适配器进行接口的强制转换
    public static void main(String[] args) {
        Apply.apply(new FilterAdaptor(new LowPass()), new WaveForm());
        Apply.apply(new FilterAdaptor(new Filter()), new WaveForm());
    }
}

class Filter{
    public String getName(){
        return this.getClass().getSimpleName();
    }

    public WaveForm filter(WaveForm waveForm){
        return waveForm;
    }
}

class LowPass extends Filter{
    @Override
    public WaveForm filter(WaveForm waveForm){
        return waveForm; // 低波过滤
    }
}


class FilterAdaptor extends Processor{
    Filter filter;

    public FilterAdaptor(Filter filter) {
        this.filter = filter;
    }

    @Override
    public String getName() {
        return super.getName() + " " + this.filter.getName();
    }

    @Override
    public Object process(Object object) {
        return filter.filter((WaveForm) object);
    }
}
